import axios from 'axios'
import { Message, Loading, Notification as Notify } from 'element-ui'
import MD5 from 'md5'
import Vue from 'vue'

Vue.$httpRequestList = [];

const http = axios.create({
    timeout: 50000
})

let load;
let count = 0;

http.interceptors.request.use(config => {
    if (!config._loading) {
        if (count === 0) load = Loading.service({
            target: document.querySelector("#app-main"),
            text: '加载中...',
            spinner: 'el-icon-loading',
            background: 'rgba(250, 250, 250, 0.5)'
        });
        count ++;
    }
    return config;
}, error => {
    Promise.resolve(error);
    count --;
    if (count === 0) load.close();
})

http.interceptors.response.use(response => {
    count --;
    if (count === 0) load.close();
    const { _msg } = response.config;
    if (!response.data.status) {
        switch (response.data.code) {
            case -404:
                return Notify.error({ title: '错误', message: response.data.message || '未知异常'})
            case -500:
                return Notify.error({ title: '错误', message: response.data.message || '服务器连接错误！'})
            default:
                Promise.reject(response.data.message || '未知异常');
                return Notify.error({ title: '错误', message: response.data.message || '服务器连接错误！'})
        }
    }

    if (_msg) {
        Message.success({
            showClose: true,
            message: response.data.message || _msg,
            type: 'success'
        })
    }
    return response.data
}, error => {
    if (!axios.isCancel(error)) {
        Promise.reject(error);
        Notify.error({ title: '错误', message: '服务器开小差啦！'})
    }
    count --;
    if (count === 0) load.close();
});

export default {
    $GET(url, params, config = {}) {
        const CancelToken = axios.CancelToken;
        const { contentType } = config;
        return http.get(url, {
            params: params,
            ...config,
            headers: {
                'Content-Type': contentType
            },
            cancelToken: new CancelToken(function executor(c) {
                setClearFnName(url, c)
            })
        })
    },

    $POST(url, params, config) {
        const CancelToken = axios.CancelToken;
        return http.post(url, params, {
            ...config,
            cancelToken: new CancelToken(function executor(c) {
                setClearFnName(url, c)
            })
        },
        )
    },

    $DELETE(url, params, config) {
        const CancelToken = axios.CancelToken;
        return http.delete(url, {
            data: params, ...config, cancelToken: new CancelToken(function executor(c) {
                setClearFnName(url, c)
            })
        })
    },

    $PUT(url, params, config) {
        const CancelToken = axios.CancelToken;
        return http.put(url, params, {
            ...config,
            cancelToken: new CancelToken(function executor(c) {
                setClearFnName(url, c)
            })
        })
    }
}


function setClearFnName(url, c) {
    let clearFn = {};
    clearFn[MD5(url)] = c;
    Vue.$httpRequestList.push(clearFn)
}
